package com.example.housemanage

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.housemanage.entity.HouseManageEntity
import com.example.housemanage.entity.HouseManageEntityItem
import com.example.lib_base.mvvm.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class HouseManageViewModel @Inject constructor(private val repo: HouseManageRepo) : BaseViewModel() {
    val propertiesResult = MutableLiveData<HouseManageEntity>() // 可根据实际类型替换 String
    fun getProperties() {
        httpRequest(
            { repo.apiService.getProperties() }, {
                propertiesResult.postValue(it)
            })
    }

    val addPropertyResult = MutableLiveData<Boolean>()
    fun addProperty(property: HouseManageEntityItem) {
        httpRequest(
            { repo.addProperty(property) },
            { addPropertyResult.postValue(true) },
            { addPropertyResult.postValue(false) }
        )
    }

    val deletePropertyResult = MutableLiveData<Boolean>()
    fun deleteProperty(id: Int) {
        httpRequest(
            { repo.deleteProperty(id) },
            { deletePropertyResult.postValue(true) },
            { deletePropertyResult.postValue(false) }
        )
    }
    
    val updatePropertyResult = MutableLiveData<Boolean>()
    /**
     * 更新房屋信息
     * @param property 更新的房屋信息
     * @return LiveData<Boolean> 更新结果
     */
    fun updateProperty(property: HouseManageEntityItem): LiveData<Boolean> {
        httpRequest(
            { repo.updateProperty(property) },
            { updatePropertyResult.postValue(true) },
            { updatePropertyResult.postValue(false) }
        )
        return updatePropertyResult
    }
}